//
//  BLETransfer.m
//  BLESDKDemo
//
//  Created by 李阳 on 2018/4/6.
//  Copyright © 2018年 李阳. All rights reserved.
//

#import "BLERequest.h"
#import "NSError+BLEAdd.h"
#import "NSArray+BLEAdd.h"

@interface BLERequest ()

/// 交互过程发生的错误
@property (nonatomic, strong) NSError *error;

#pragma mark Response

/// 响应数据的长度
@property (nonatomic, assign) NSInteger responseDataLength;
/// 实际响应的次数
@property (nonatomic, assign) NSInteger responseTimes;
/// 响应的数据
@property (nonatomic, strong) NSData *responseData;
/// 响应的数据数组 已做好截取
@property (nonatomic, strong) NSArray<NSData *> *responseDatas;

@end

@implementation BLERequest

- (void)setShouldResponseTimes:(NSInteger)times
{
    _shouldResponseTimes = times;
    NSTimeInterval timeout = times * 0.4;
    if (timeout > _timeout)
    {
        _timeout = timeout;
    }
}


#pragma mark - NSObject

- (instancetype)initWithSentData:(NSData *)data
{
    self = [self init];
    if (!self) return nil;
    self.sentData = data;
    return self;
}
- (instancetype)initWithSentData:(NSData *)data
                            type:(BLERequestType)type
{
    self = [self initWithSentData:data];
    if (!self) return nil;
    self.type = type;
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (!self) return nil;
    _timeout = 10;
    return self;
}


- (BOOL)isEqualToRequest:(BLERequest *)request
{
    if (self.sentData.length != request.sentData.length) return NO;
    Byte *byteSelf = (Byte *)self.sentData.bytes;
    Byte *byteModel = (Byte *)request.sentData.bytes;
    
    for (NSInteger i = 0; i < self.sentData.length; i++)
    {
        if (byteSelf[i] != byteModel[i])
        {
            return NO;
        }
    }
    return YES;
}

- (BOOL)isEqual:(id)object
{
    if (self == object) return YES;
    if (![object isKindOfClass:[self class]]) return NO;
    return [self isEqualToRequest:object];
}

- (NSUInteger)hash
{
    return self.sentData.hash;
}

@end
